package com.itheima.MyFilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(value ={"/stu/*","*.jsp"})
public class LoginFilter implements Filter {
    //过滤器 过滤掉一些重复的登录代码 提高代码复用性
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;


        Object username = req.getSession().getAttribute("username");
        if(username ==null){
            //重定向到登录页面
            resp.sendRedirect(req.getContextPath()+"/Login.html");

        }else{
            // 登录了 就放行
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }
}
